package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetMealDTO;
import com.sky.dto.SetMealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.SetMeal;
import com.sky.entity.SetMealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetMealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * sky-take-out
 * com.sky.service.impl <br>
 * 套餐Service实现类
 * @author Ablecisi
 * @version 1.0
 * 2025/3/28
 * 17:06
 */
@Service
public class SetMealServiceImpl implements SetMealService {
    private final SetMealMapper setMealMapper;
    private final SetMealDishMapper setMealDishMapper;
    private final DishMapper dishMapper;

    @Autowired
    public SetMealServiceImpl(SetMealMapper setMealMapper, SetMealDishMapper setMealDishMapper, DishMapper dishMapper) {
        this.setMealMapper = setMealMapper;
        this.setMealDishMapper = setMealDishMapper;
        this.dishMapper = dishMapper;
    }

    /**
     * 套餐分页查询
     * @param setMealPageQueryDTO 套餐分页查询参数
     * @return 套餐分页查询结果
     */
    @Override
    public PageResult<SetMealVO> pageQuerySetMeal(SetMealPageQueryDTO setMealPageQueryDTO) {
        Page<SetMealVO> startPage = PageHelper.startPage(setMealPageQueryDTO.getPage(),setMealPageQueryDTO.getPageSize());
        SetMeal setMeal = new SetMeal();
        BeanUtils.copyProperties(setMealPageQueryDTO,setMeal);
        Page<SetMealVO> setMeals = setMealMapper.querySetMeal(setMeal);
        startPage.close();
        return new PageResult<>(setMeals.getTotal(), setMeals.getResult());
    }

    /**
     * 添加套餐
     * @param setMealDTO 套餐参数
     */
    @Override
    @Transactional
    public void addSetMeal(SetMealDTO setMealDTO) {
        SetMeal setMeal = new SetMeal();
        BeanUtils.copyProperties(setMealDTO, setMeal);
        // 添加套餐
        setMealMapper.insertSetMeal(setMeal);
        // 添加套餐菜品关系
        List<SetMealDish> setMealDishes = setMealDTO.getSetmealDishes();
        if (setMealDishes != null && !setMealDishes.isEmpty()) {
            setMealDishes.forEach(setMealDish -> setMealDish.setSetmealId(setMeal.getId()));
            System.out.println(setMealDishes);
            setMealDishMapper.insertSetMealDish(setMealDishes);
        }
    }

    /**
     * 删除套餐
     * @param setMealIds 套餐id
     */
    @Override
    @Transactional
    public void deleteSetMeal(List<Long> setMealIds) {
        setMealIds.forEach(id -> {
            SetMeal setmeal = setMealMapper.querySetMealById(id);
            if(StatusConstant.ENABLE.equals(setmeal.getStatus())){
                //起售中的套餐不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        setMealMapper.deleteSetMeal(setMealIds);
        // 删除套餐菜品关系
        setMealDishMapper.deleteSetMealDishBySetMealIds(setMealIds);
    }

    /**
     * 根据套餐id查询套餐
     * @param setMealId 套餐id
     * @return 套餐
     */
    @Override
    @Transactional
    public SetMealVO querySetMealById(Long setMealId) {
        // 先查询套餐和套餐关系的菜品
        List<SetMealDish> setMealDishes = setMealDishMapper.queryDishBySetMealId(setMealId);
        SetMeal setMeal = setMealMapper.querySetMealById(setMealId);

        SetMealVO setMealVO = new SetMealVO();
        BeanUtils.copyProperties(setMeal, setMealVO);
        setMealVO.setSetmealDishes(setMealDishes);

        return setMealVO;
    }

    /**
     * 更新套餐
     * @param setMealDTO 套餐参数
     */
    @Override
    @Transactional
    public void updateSetMeal(SetMealDTO setMealDTO) {
        SetMeal setMeal = new SetMeal();
        BeanUtils.copyProperties(setMealDTO, setMeal);
        // 更新套餐
        setMealMapper.updateSetMeal(setMeal);
        // 添加套餐菜品关系
        List<SetMealDish> setMealDishes = setMealDTO.getSetmealDishes();
        if (setMealDishes != null && !setMealDishes.isEmpty()) {
            // 删除套餐菜品关系
            setMealDishMapper.deleteSetMealDishBySetMealIds(new ArrayList<Long>(){{add(setMeal.getId());}});
            setMealDishes.forEach(setMealDish -> setMealDish.setSetmealId(setMeal.getId()));
            setMealDishMapper.insertSetMealDish(setMealDishes);
        }
    }

    /**
     * 更新套餐状态
     * @param setMealId 套餐id
     * @param status 套餐状态
     */
    @Override
    public void updateSetMealStatus(Long setMealId, Integer status) {
        //起售套餐时，判断套餐内是否有停售菜品，有停售菜品提示"套餐内包含未启售菜品，无法启售"
        if(StatusConstant.ENABLE.equals(status)){
            //select a.* from dish a left join setmeal_dish b on a.id = b.dish_id where b.setmeal_id = ?
            List<Dish> dishList = dishMapper.getBySetMealId(setMealId);
            if(dishList != null && !dishList.isEmpty()){
                dishList.forEach(dish -> {
                    if(StatusConstant.DISABLE.equals(dish.getStatus())){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }

        SetMeal setmeal = SetMeal.builder()
                .id(setMealId)
                .status(status)
                .build();
        setMealMapper.updateSetMeal(setmeal);
    }

    /**
     * 根据分类id查询套餐
     * @param setMeal 套餐参数
     * @return 套餐列表
     */
    @Override
    public List<SetMeal> querySetMealByCategoryId(SetMeal setMeal) {
        return setMealMapper. querySetMealByCategoryId(setMeal);
    }

    /**
     * 根据套餐id查询套餐菜品
     * @param setMealId 套餐id
     * @return 菜品列表
     */
    @Override
    public List<DishItemVO> getDishItemById(Long setMealId) {
        return setMealMapper.getDishItemBySetMealId(setMealId);

    }
}
